<template>
	<view class="approve-box">
		<view class="form-box">
			<container-box title="基本信息（必填）">
				<form-item title="义务人类型" type="other">
					<view class="type-buttons">
						<!-- :disabled="personType" -->
						<button @click="typeButtonClicked(1)"  class="type-button"
							:class="form.applyType==1?'type-select':'type-default'" style="margin-right: 10px;">自然人</button>
						<button @click="typeButtonClicked(3)"  class="type-button"
							:class="form.applyType==3?'type-select':'type-default'">法人</button>
					</view>
				</form-item>
				<!-- :disabledIpt="true" -->
				<view class="person" v-show="form.applyType==1">
					<form-item title="业务人姓名" :isRequired="true" :disabledIpt="false"  v-model="form.ywrMc"></form-item>
					<form-item title="证件类型" :isRequired="true"  :disabledIpt="false" v-model="form.zjlx"></form-item>
					<form-item title="证件号码" :isRequired="true"  :disabledIpt="false" v-model="form.ywrZjh"></form-item>
					<form-item title="联系电话" :isRequired="true"  :disabledIpt="false" v-model="form.ywrPhone"></form-item>
					<form-item title="申请验收面积" :isRequired="true" type="number" v-model="form.sqysmj"></form-item>
					<form-item title="申请复垦项目" :isRequired="true" v-model="form.projectName" @input='inputProject'>
						<button class="small-button" @click='selectProject'>选择</button>
					</form-item>
				</view>
				<view class="person" v-show="form.applyType==3">
					<form-item title="企业名称" :isRequired="true" :disabledIpt="false" v-model='form.companyName'></form-item>
					<form-item title="社会投资代码" :isRequired="true" :disabledIpt="false" v-model="form.creditCode"></form-item>
					<form-item title="法人代表姓名" :isRequired="true" :disabledIpt="false" v-model="form.ywrMc"></form-item>
					<form-item title="法人代表身份证号" :isRequired="true" :disabledIpt="false" v-model='form.ywrZjh'></form-item>
					<form-item title="联系电话" :isRequired="true"  v-model="form.ywrPhone"></form-item>
					<form-item title="申请验收面积" :isRequired="true" type="number" v-model="form.sqysmj"></form-item>
					<form-item title="申请复垦项目" :isRequired="true" v-model="form.projectName" @input='inputProject'>
						<button class="small-button" @click='selectProject'>选择</button>
					</form-item>
					<!-- <form-item v-else :message="form.projectName" title="申请复垦项目" :isRequired="true" type="select"
						placeholder="请选择项目" right="arrow" @click='selectProject'></form-item> -->
				</view>
				<view class="project-info" v-if="isShowProjectInfo&&form.applyType==1">
					<info-item title="项目名称" :message="projectInfo.projectName"></info-item>
					<info-item title="用地位置" :message="projectInfo.ydwz"></info-item>
					<info-item title="批准文号" :message="projectInfo.pzwh" type="text"></info-item>
					<info-item title="区县名称" :message="projectInfo.qxmc" type="text"></info-item>
					<info-item title="批复总面积" :message="projectInfo.pfmj" type="text"></info-item>
					<info-item title="批准日期" :message="projectInfo.pzrq" type="text"></info-item>
					<info-item title="到期时间" :message="projectInfo.xmsyqxz" type="text"></info-item>
				</view>
			</container-box>
			<container-box title="附件上传（*必填）" style="">
				<file-item title="土地复垦验收申请" files='文件名称'></file-item>
				<file-item title="土地复垦方案和工程规划设计批文件" files='文件名称'></file-item>
				<file-item title="土地工程实施前后对比照片" files='文件名称'></file-item>
				<file-item title="交还土地佐证资料" files='文件名称'></file-item>
				<file-item title="竣工图" files='文件名称'></file-item>
				<file-item title="政府代复垦相关资料" files='文件名称' :isRequired="false"></file-item>
				<file-item title="建设用地批文" :isRequired="false"></file-item>
				<file-item title="坐标串txt文件"></file-item>
				<file-item title="其他" :isRequired="false"></file-item>
			</container-box>
			<container-box title="线上填报地址">
				<div style="padding: 5px;">
					<uni-link 
					href="https://lsydsq.cqlandcloud.org.cn/lsyd_sqxx/#/" 
					text="https://lsydsq.cqlandcloud.org.cn/lsyd_sqxx/#/"></uni-link>
				</div>
			</container-box>
		</view>
		<form-footer @submitClicked='submitClicked'></form-footer>
		<project-alert ref="projectalert" @confirm='confirm' @addOther='addOtherProject'></project-alert>
	</view>
</template>

<script>
	import FormItem from '@/components/FromItem.vue'
	import FormFooter from '@/components/FormFooterView.vue'
	import ContainerBox from '@/components/ContainerBox.vue'
	import InfoItem from '@/components/InfoItem.vue'
	import FileItem from '@/components/FileItem.vue'
	import ProjectAlert from '@/components/ProjectAlert.vue'
	import request from '@/utils/request.js'
	import {
		uploadFile
	} from '@/utils/uploadFile.js'
	export default {
		data() {
			return {
				personType: false, //0自然人 1法人
				currentType: 1,
				form: {
					// use: '',
					// address: '',
					// applyType: 1,
					// sfdxyzc: 0,
					// tdcbjyqlz: 0,
					id:'',
					ywrMc:"",
					ywrZjlx:"",
					ywrZjh:"",
					ywrPhone:"",
					applyType:1,
					companyName:"",
					creditCode:"",
				},
				isShowProjectInfo: false,
				projectInfo:{},
				isAddOtherProject:false,//判断是否手动录入项目
			};
		},
		components: {
			FormItem,
			FormFooter,
			ContainerBox,
			InfoItem,
			FileItem,
			ProjectAlert
		},
		onLoad() {
			
			this.$nextTick(() => {
				// 	this.$refs.noticeAlert.show()
				
				let self = this
				var obj = {}
				var userInfo = {}
				obj =uni.getStorageSync('userCurrentQy')? JSON.parse(uni.getStorageSync('userCurrentQy')):{}
				userInfo =uni.getStorageSync('userInfo')? JSON.parse(uni.getStorageSync('userInfo')):{}
				self.form.ywrMc = userInfo.userVerification&&userInfo.userVerification.userPersonName
				self.form.ywrZjh = userInfo.userVerification&&userInfo.userVerification.userPersonId
				self.form.ywrZjlx = userInfo.userVerification&&userInfo.userVerification.userPersonIdType
				self.form.zjlx = userInfo.userVerification&&userInfo.userVerification.userPersonIdType==1?'身份证':''
				self.form.applyType =  uni.getStorageSync('userType')
				self.form.ywrPhone = userInfo.phone
				
				self.form.companyName = obj.orgName
				self.form.creditCode = obj.socialCreditCode
				
				console.log(self.form,'olLoad')
				this.$forceUpdate()
			})

		},
		methods: {
			inputProject(){
				this.form.id = ''
				debugger
			},
			addOtherProject(){
				this.isAddOtherProject = true
				this.form.id = ''
				this.form.projectName = ''
			},
			typeButtonClicked(val) {
				this.form.applyType = val
				console.log(val, 'val')
				if (val == 1) {
					// this.form.projectName = `${this.form.applicantName||''}${this.form.use || ''}`

				} else if (val == 3) {
					// this.form.projectName = `${this.form.applicantName||''}${this.form.use || ''}`
				}

			},
			selectProject() {
				this.$refs.projectalert.show()
			},
			confirm(confirmData) {
				if(!confirmData){
					return
				}
				this.isShowProjectInfo = true
				// this.form.xm = confirmData
				this.$set(this.form, 'projectName', confirmData.projectName)
				this.$set(this.form, 'id', confirmData.id)
				this.$set(this.projectInfo,confirmData)

				this.projectInfo = confirmData
				this.form.sqysmj = confirmData.sqysmj
				console.log(confirmData, 'confirm')
			},
			checkForm(){
				let keys = Object.keys(this.form)
				for (var i = 0; i < keys.length; i++) {
					if (!this.form[keys[i]]) {
						uni.showToast({
							title:`请输入${keys[i]}`,
							icon:'none'
						})
						return false
					}
				}
				return true
			},
			submitClicked() {
				
				let self = this
				// let ram = Math.ceil(Math.random() * 10e5).toString(5)
				let timeS = Date.now()
				let userInfo = uni.getStorageSync('userInfo')
				console.log(userInfo, 'userInfo')
				this.form.userId = userInfo.id
				this.form.projectNo = timeS
				request.post('/lsydfk/applies/add/new', this.form).then(res => {
					self.form = {
						id:'',
						ywrMc:"",
						ywrZjlx:"",
						zjlx:"",
						ywrZjh:"",
						ywrPhone:"",
						applyType:1,
						companyName:"",
						creditCode:"",
					}
					uni.navigateTo({
						url: '/pages/PageApplyResult?code=' + res
					})
					console.log(res,'res')
				}).catch(err => {
					uni.showToast({
						title: err,
						icon: 'error'
					})
				})
				return
				uni.navigateTo({
					url: '/pages/PageApplyResult'
				})
			},

		},
	}
</script>

<style lang="scss">
	.small-button{
		background-color: #1E92F0;
		color: #FFFFFF;
		font-size: 28rpx;
		padding: 5px;
		line-height: 28rpx;
		margin-left: 10rpx;
	}
	.project-info {
		background-color: #F2F3F5;
		margin-top: 10rpx;
	}
	.container-box{
		margin-bottom: 20px;
	}
	.form-box {
		margin: 20rpx;
		border-radius: 16rpx;
		background-color: #fff;
		padding:30rpx 20rpx;
		padding-bottom: 150rpx;
	}

	.type-buttons {
		display: flex;
		justify-content: space-between;
	}

	.area-box {
		display: flex;
		justify-content: flex-end;
		color: red;
		font-size: 26rpx;
	}

	.type-button {
		// width: 81px;
		flex: 1;
		height: 33px;
		font-size: 28rpx;
		line-height: 31px;
		background-size: 100%;
		background-repeat: no-repeat;
		// margin-right: 10rpx;
	}

	.type-select {
		border: 1rpx solid #1E92F0;
		background: #F1F9FB;
		border-radius: 6px 6px 6px 6px;
		color: #1E92F0;
	}

	.type-default {
		height: 31px;
		line-height: 30px;
		background: #FFFFFF;
		border-radius: 6px 6px 6px 6px;
		border: 1rpx solid #D8D8D8;
		color: #333333;
	}
</style>